package com.xiao.common.util;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author xiaoxiaoping
 * @Date 2020/2/3 15:42
 * @Version 1.0
 */
public class BeanUtil {
    /**
     * 复制集合
     */
    public static <T,K> List<T> copyList(List<K> sourceList, Class<T> clazz) {
        if (CollectionUtils.isEmpty(sourceList)) {
            return null;
        }

        ArrayList<T> target = new ArrayList<>();
        sourceList.forEach(k -> target.add(copyBean(k, clazz)));
        return target;
    }

    /**
     * 复制对象
     */
    public static <T,K> T copyBean(K source, Class<T> clazz) {
        T t = BeanUtils.instantiate(clazz);
        BeanUtils.copyProperties(source, t);
        return t;
    }
}
